package org.testis.webautomationmodule.ui.objects;

import org.testis.data.AtofModuleConstants;
import org.testis.logging.AtofLogLevel;
import org.testis.webautomationmodule.ui.UIFormFieldDecorator;

/**
 *
 * @author nurhat
 */
public class UICheckBoxGroup extends UIFormFieldDecorator {

    private String[] m_valueList;

    public UICheckBoxGroup(String id) {
        super(id);
    }

    public UICheckBoxGroup(String id,
            String value) {
        super(id,
                value);
        setValueList(value.split(
                AtofModuleConstants.PARAM_SEP_SPLIT_STR));
    }

    @Override
    protected boolean setBehavior() {
        putLog("Clearing all checkboxes exists in CheckBoxGroup Object with "
                + getId()
                + " id  ...");
        getWebAutomationImpl().getEval(
                "function atof_uncheckAll(){var options=selenium.browserbot.getDocument().getElementsByName('"
                + getId() + "'); "
                + "for (i = 0; i < options.length; i++){"
                + "    options[i].checked = false;"
                + "} return 'ok'}; eval('atof_uncheckAll()');");
        for (int i = 0; i < m_valueList.length; i++) {
            UICheckBox chk = new UICheckBox(m_valueList[i],
                    "1");
            chk.setMessageId(getMessageId());
            chk.setWebAutomationImpl(this.getWebAutomationImpl());
            chk.setBehavior();
        }
        return true;
    }

    @Override
    public boolean verifyBehavior() {
        putLog(" Verifying CheckBoxGroup Object with " + getId()
                + " id  ...");
        if (getValue().startsWith(AtofModuleConstants.CONTAINS_STR)) {
            return checkContainsValidation();
        } else if (getValue().startsWith("!=" + AtofModuleConstants.CONTAINS_STR)) {
            return !checkContainsValidation();
        } else if (getValue().startsWith(AtofModuleConstants.LIST_BOX_EMPTY_STR)) {
            return checkEmtpyValidation();
        } else if (getValue().startsWith("!=" + AtofModuleConstants.LIST_BOX_EMPTY_STR)) {
            return !checkEmtpyValidation();
        }
        String checkedObjects = getSelectedObjects();
        String[] checkedObjectsList = checkedObjects.split(
                AtofModuleConstants.PARAM_SEP_SPLIT_STR);
        boolean verified = true;
        for (int i = 0; i < getValueList().length; i++) {
            boolean isFound = false;
            for (int j = 0; j < checkedObjectsList.length; j++) {
                if (checkedObjectsList[j].equals(getValueList()[i])) {
                    isFound = true;
                    break;
                }
            }
            if (!isFound) {
                verified = false;
                break;
            }
        }
        if (verified) {
            putLog("  CheckBoxGroup Object with " + getId()
                    + " id  was verified...curcheckedObjects:" + checkedObjects
                    + "  compared with:" + getValue());
            setValue(checkedObjects);
            return true;
        } else {
            putLog("  CheckBoxGroup Object with " + getId()
                    + "  id  was not verified...curcheckedObjects:"
                    + checkedObjects
                    + "  compared with:" + getValue(),
                    AtofLogLevel.ERROR);
            setValue(checkedObjects);
        }

        return false;
    }

    @Override
    public boolean getBehavior() {

        setValue(getSelectedObjects());
        putLog("Gets value of CheckBoxGroup with id="
                + getId() + " value=" + getValue());
        return true;
    }

    public String[] getValueList() {
        return m_valueList;
    }

    public void setValueList(String[] valueList) {
        this.m_valueList = valueList;
    }

    public String getSelectedObjects() {
        String curselectedObjects = getWebAutomationImpl().getEval(
                "function atof_getCheckedValues(){var options=selenium.browserbot.getDocument().getElementsByName('"
                + getId() + "'); "
                + " var objects='';"
                + " for (i = 0; i < options.length; i++){"
                + "   if(options[i].checked) "
                + "   { "
                + "     if(objects=='') "
                + "        objects=options[i].id;"
                + "     else "
                + "        objects=checkedObjects + '"
                + AtofModuleConstants.PARAM_SEP_STR
                + "'+options[i].id;"
                + "   }"
                + " } "
                + " return objects"
                + "}; "
                + "eval('atof_getCheckedValues()');");
        return curselectedObjects;
    }

    public String getAllObjects() {
        String curselectedObjects = getWebAutomationImpl().getEval(
                "function atof_getAllValues(){var options=selenium.browserbot.getDocument().getElementsByName('"
                + getId() + "'); "
                + " var objects='';"
                + " for (i = 0; i < options.length; i++){"
                + "     if(objects=='') "
                + "        objects=options[i].id;"
                + "     else "
                + "        objects=checkedObjects + '"
                + AtofModuleConstants.PARAM_SEP_STR
                + "'+options[i].id;"
                + " } "
                + " return objects"
                + "}; "
                + "eval('atof_getAllValues()');");
        return curselectedObjects;
    }

    private boolean checkEmtpyValidation() {
        String allObjects = getAllObjects();
        if (allObjects.isEmpty()) {
            return true;
        }
        return false;
    }

    private boolean checkContainsValidation() {
        String values = "";
        if (getValue().startsWith("!=")) {
            values = getValue().replace("!=" + AtofModuleConstants.CONTAINS_STR,
                    "");
        } else {
            values = getValue().replace(AtofModuleConstants.CONTAINS_STR,
                    "");
        }
        int startIdx = values.indexOf("(");
        int endIdx = values.lastIndexOf(")");
        if (startIdx >= 0 && endIdx > 0) {
            values = values.substring(startIdx + 1,
                    endIdx);
            String[] valueList = values.split(
                    AtofModuleConstants.PARAM_SEP_SPLIT_STR);
            String allObjects = getAllObjects();
            String[] objectsList = allObjects.split(
                    AtofModuleConstants.PARAM_SEP_SPLIT_STR);
            boolean verified = true;
            for (int i = 0; i < valueList.length; i++) {
                boolean isFound = false;
                for (int j = 0; j < objectsList.length; j++) {
                    if (objectsList[j].equals(valueList[i])) {
                        isFound = true;
                        break;
                    }
                }
                if (!isFound) {
                    verified = false;
                    break;
                }
            }

            if (verified) {
                putLog("  CheckBoxGroup Object with " + getId()
                        + " id  was verified...curObjects:"
                        + allObjects
                        + "  compared with:" + getValue());
                setValue(allObjects);
                return true;
            } else {
                putLog("  CheckBoxGroup Object with " + getId()
                        + " id  was not verified...curObjects:"
                        + allObjects
                        + "  compared with:" + getValue(),
                        AtofLogLevel.ERROR);
                setValue(allObjects);
            }


            return true;
        } else {
            putLog("  CheckBoxGroup Object with " + getId()
                    + " id  was not verified...Given value parameter is not valid for contains statement:"
                    + getValue(),
                    AtofLogLevel.ERROR);
            return false;
        }
    }
}
